package com.uyun.rbac.user.web;

import com.uyun.rbac.permission.entity.GroupEntity;
import com.uyun.rbac.permission.entity.UserEntity;
import com.uyun.rbac.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
public class UserController  extends MultiActionController {
 
	@Autowired
	private UserService userService;

	@RequestMapping( value="/user/index")
	public ModelAndView index(){
 		
		return new ModelAndView("user/index");
	}
	
	@RequestMapping( value="/user/getList",method = RequestMethod.GET)
	public @ResponseBody
	ResponseEntity<List<UserEntity>> getList(@RequestParam String userName,
											 @RequestParam String groups,
											 @RequestParam String status){


        Map<String,Object> paraMap = new HashMap<>();
        paraMap.put("userName",userName);
        paraMap.put("groups",groups);
        paraMap.put("status",status);
 		List<UserEntity> list = userService.getUserList(paraMap);
        return new ResponseEntity<>(list, HttpStatus.OK);
 
	}


	@RequestMapping( value="/user/getUserGroups",method = RequestMethod.GET)
	public @ResponseBody
	ResponseEntity<List<GroupEntity>> getUserGroups(){

		List<GroupEntity> list = userService.getAllUserGroup();
		return new ResponseEntity<>(list, HttpStatus.OK);
	}

    @RequestMapping( value="/user/editGroup",method = RequestMethod.GET)
    public @ResponseBody
	ResponseEntity<String> editGroup(@RequestParam String userId,
									 @RequestParam String groupId){

        String code ="0";
        if(userService.editUserGroup(userId,groupId)){
            code="1";
        }

        return new ResponseEntity<>(code, HttpStatus.OK);
    }
	
	 
}
